//
//  WBOAuthViewController.m
//  SinaWeibo2
//
//  Created by zhuangqianwei on 16/12/1.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

#import "WBOAuthViewController.h"
#import <SVProgressHUD.h>
@interface WBOAuthViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webView;

@end

@implementation WBOAuthViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStyleDone target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(fillAccount)];
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
    _webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:req];
}
- (void)close {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)fillAccount {
    
    NSString *jsStr = @"document.getElementById('userId').value = '18550005398', document.getElementById('passwd').value = '307518'";

    [_webView stringByEvaluatingJavaScriptFromString:jsStr];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}
#pragma mark - 网页的代理方法
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    NSString *urlString = 
    
    return  YES;
}



@end
